package com.lemon.smartsafety.ui.adapter.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.data.app.DataUtil;
import com.lemon.smartsafety.entity.event.main.MainNewsColumnEvent;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafety.utils.common.DrawableUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.ViewUtils;
import com.lemon.safecloud.greendao.NewsColumn;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/1.
 */
public class MainNewsColumnItemView implements AdapterItem<MainRcvItemBase<List<NewsColumn>>> {
    private LoggerHelper loggerHelper = new LoggerHelper(MainNewsColumnItemView.class);

    @Bind(R.id.gridView)
    GridView mGridView;

    private Context mContext;

    private int columnNumber = Constants.MAIN_PANEL_NEWS_COLUMN;
    private int leftMargin   = 10;
    private int topMargin    = 0;

    @Override
    public int getLayoutResId() {
        return R.layout.item_no_scroll_gridview;
    }

    @Override
    public void bindViews(View root) {
        ButterKnife.bind(this, root);
    }

    @Override
    public void setViews() {
    }

    @Override
    public void handleData( MainRcvItemBase<List<NewsColumn>> mainRcvItemBase, int position) {
        mContext = mGridView.getContext();
        if (RcvViewType.MainRcvViewType.TYPE_NEWS_COLUMN != mainRcvItemBase.getViewType()) {
            return;
        }
        int                      screenWidth = ViewUtils.getScreenWidth(mContext);
        int                      itemWidth   = (int) (screenWidth / columnNumber) - 1;
        final List<NewsColumn> items       = mainRcvItemBase.getData();


        GridViewAdapter adapter = new GridViewAdapter(mContext, items);
//        mGridView.setBackgroundColor(mContext.getResources().getColor(R.color.md_amber));
        mGridView.setNumColumns(columnNumber);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MainNewsColumnEvent event = new MainNewsColumnEvent();
                event.setTag(EventTag.TAG_MAIN_NEWS_COLUMN_EVENT);
                event.setIndex(position);
                event.setData(items.get(position));
                AppService.getBus().post(event);
            }
        });
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(screenWidth - (ViewUtils.dip2px(mContext, leftMargin * 2)), RecyclerView.LayoutParams.WRAP_CONTENT);
        params.setMargins(ViewUtils.dip2px(mContext, leftMargin), ViewUtils.dip2px(mContext, topMargin), ViewUtils.dip2px(mContext, leftMargin), 0);

        mGridView.setLayoutParams(params);

    }


    /**
     * 主界面的按钮视图Adapter
     */
    class GridViewAdapter extends BaseAdapter {

        private Context            mContext;
        private List<NewsColumn> items;


        public GridViewAdapter(Context context, List<NewsColumn> items) {
            this.mContext = context;
            this.items = items;

        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return DataUtil.getMainUIButtons().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_news_column2, null);
                /*根据parent动态设置convertview的大小*/
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
                /*解决动态设置convertview大小，第一项不显示的BUG*/
            }

            switch (position) {
                case 0:{
                    holder.ivIcon.setImageResource(R.mipmap.ic_file_blocks);
                    break;
                }
                case 1:{
                    holder.ivIcon.setImageResource(R.mipmap.ic_share_files);
                    break;
                }
                case 2:{
                    holder.ivIcon.setImageResource(R.mipmap.ic_change_file);
                    break;
                }
                case 3:{
                    holder.ivIcon.setImageResource(R.mipmap.ic_copy_file_1);
                    break;
                }
                default:{
                    holder.ivIcon.setImageResource(R.mipmap.ic_file_blocks);
                    break;
                }
            }

            NewsColumn item        = items.get(position);
            Drawable     src         = holder.ivIcon.getDrawable();
            int          randomColor = DrawableUtils.getRandomColor(mContext.getResources().getStringArray(R.array.material_design_colors));


            holder.ivIcon.setImageDrawable(DrawableUtils.$tint(src, randomColor));
            holder.tvTitle.setText(item.getTitle());
            holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.colorTextPrimary));

            return convertView;
        }


        class ViewHolder {
            LinearLayout mainLinearLayout;
            TextView     tvTitle;
            @Nullable
            TextView tvSubTitle;
            ImageView ivIcon;

            ViewHolder(View convertView) {
                mainLinearLayout = (LinearLayout) convertView.findViewById(R.id.mainLinearLayout);
                tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                tvSubTitle = (TextView) convertView.findViewById(R.id.tvSubTitle);
                ivIcon = (ImageView) convertView.findViewById(R.id.imageIcon);
            }
        }

        public int[] getImageWH(int Res) {
            Bitmap bitmap = BitmapFactory.decodeResource(this.mContext.getResources(), Res);
            int    height = bitmap.getHeight();
            int    width  = bitmap.getWidth();
            bitmap = null;
            int[] result = {width, height};
            return result;
        }
    }


}
